var SWImportStatic = {
	S_NoImport:0,
	S_Importing:1,
	S_Imported:2
}
var SWImportUtil={
	localPrefix:"local:",
	localCtx:false,
	readFun:[],
	ctx:false,
	needInput:{},
	modelMap:{},
	importCnt:0,
	finished:true,
	formatJsPath:function(jsPath){
		if(!jsPath) return "";
		if(jsPath.indexOf("\\")!=-1){
			jsPath = jsPath.replace("\\","/");
		}
		
		return jsPath;
	},
	/**
	 * 增加导入
	 */
	addImport:function(jsPath){
		jsPath=SWImportUtil.formatJsPath(jsPath);
		var swimport = new SWImport(jsPath);
		jsPath = swimport.getJsPath();
		if(SWImportUtil.needInput[jsPath]){
			return;
		}
		SWImportUtil.needInput[jsPath ] = swimport;
		SWImportUtil.importCnt++;
		swimport.doImport();
	},
	
	
	finishImport:function(jsPath){
		jsPath=SWImportUtil.formatJsPath(jsPath);
		var swimport = SWImportUtil.needInput[jsPath];
		if(swimport){
			
			if(swimport.doFinishImport())
				SWImportUtil.importCnt--;
		}
		if(SWImportUtil.importCnt==0){
			//SWImportUtil.finished = true;
			SWImportUtil.finished = SWClassUtil.doAllExtends();
			if( SWImportUtil.readFun.length>0){
				var len = SWImportUtil.readFun.length;
				for(var i=0;i<len;i++){
					SWImportUtil.readFun[i]();
				}
				SWImportUtil.readFun.length = 0;
			}
		}
		//console.log("end:" + SWImportUtil.importCnt);
	},
	/**
	 * 根据模块名返回js文件
	 */
	getJsPathByModel:function(model){
		var path = SWImportUtil.modelMap[model];
		
		if(path){
			
			if(path.indexOf(SWImportUtil.localPrefix)==0){
				return SWImportUtil.getLocalCtx() + path.substring(SWImportUtil.localPrefix.length);
			}else{
				return SWImportUtil.getCtx()  + path;
			}
		}
		return SWImportUtil.getCtx()  +  model+".js";
	},
	getLocalCtx:function(){

		if(!SWImportUtil.localCtx){
			var ctx = SWImportUtil.getCtx();
			if(ctx.indexOf('http')!=0){
				SWImportUtil.localCtx = ctx;
			}else{
				SWImportUtil.localCtx="";
			}
		}
		return SWImportUtil.localCtx;
	},
	/**
	 * 返回上下文
	 */
	getCtx:function(){
		if(!SWImportUtil.ctx){
			var script = document.getElementById("swjs");
			if(script){
				var src = script.src;
				if(!src){
					SWImportUtil.ctx="";
				}else{
					if(src.length>10)
						SWImportUtil.ctx=src.substring(0,src.length-10);
					else
						SWImportUtil.ctx="";
				}
			}else{
				SWImportUtil.ctx="";
			}
		}
		if(SWImportUtil.ctx=="")
			return "";
		return SWImportUtil.ctx+"/";
	}
}
/**
 * 一个导入
 */
function SWImport(jsPath){
	this.jsPath = jsPath;
	this.state = SWImportStatic.S_NoImport;
}
/**
 * 做更新
 */
SWImport.prototype.doImport = function() {
	if(this.state != SWImportStatic.S_NoImport) return;
	var script = document.createElement("script");
	script.setAttribute("src",this.getJsPath());
	var jsPath = this.getJsPath();
	script.onload = function(){
		$finish(jsPath);
	}
	script.onreadystatechange = function(){
		if (script.readyState=="loaded" || script.readyState=="complete"){
			$finish(jsPath);
		}
	}
	if(document.body){
		document.body.appendChild(script);
	}else{
		var head = document.getElementsByTagName("head")[0];
		if(head){
			head.appendChild(script);
		}
	}
	this.state = SWImportStatic.S_Importing;
}
/**
 * 得到js的路径
 */
SWImport.prototype.getJsPath=function(){
	//如果http开头直接加载
	if(this.jsPath && this.jsPath.substr(0,7).toLowerCase()=='http://'){
		return this.jsPath;
	}
	if(this.jsPath  ){
		return SWImportUtil.getJsPathByModel(this.jsPath);
	}
	return SWImportUtil.getCtx() + this.jsPath;
}
/**
 * 完成加载
 */
SWImport.prototype.doFinishImport = function(){
	if(this.state != SWImportStatic.S_Imported){
		this.state = SWImportStatic.S_Imported;
		return true;
	}else{
		return false;
	}	
}
/**
 * 
 */
SWClassUtil = {
	needntExtend:{
		toString:true,
		__parentClassName:true,
		__className:true,
		con:true
	},
	classMap:{},
	rootClass:[],
	classCnt:0,
	/**
	 * 执行所有类的继承操作
	 */
	doAllExtends:function(){
		var rootClass = SWClassUtil.rootClass;
		for(var i=rootClass.length-1;i>=0;i--){
			rootClass[i].__doExtend();
		}
		return SWClassUtil.checkExtends();
		
		
	},
	/**
	 * 检查类是否全部被继承
	 */
	checkExtends:function(){
		if(SWClassUtil.classCnt>0){
			var classMap = SWClassUtil.classMap;
			for(var e in classMap){
				if(!classMap[e].__inited){
					return false;
					//alert(classMap[e].__className+'没有被初始化，可能是因为' + classMap[e].__parentClassName+'没有导入');
				}
			}
		}
		return true;
	},
	/**
	 * 注册
	 */
	reg:function(swclass){
		if(!swclass) 
			return;
		SWClassUtil.classCnt++;
		
		var parentClass = SWClassUtil.getClassByName(swclass.__parentClassName);
		if(parentClass)
			parentClass.__addChildClass(swclass);
		
		var oldClass = SWClassUtil.getClassByName(swclass.__className);
		
		if(oldClass){
			var children = oldClass.__children;
			for(var i=children.length-1;i>=0;i--){
				swclass.__addChildClass(children[i]);
			}
		}
		SWClassUtil.classMap[swclass.__className]=swclass;
		if(!swclass.__parentClassName){
			SWClassUtil.rootClass.push(swclass);
		}
	},
	/**
	 * 建立类
	 */
	createClass : function(className,fun,parentClassName) {
		
		var ret = function(p1,p2,p3,p4,p5) {
			if(this[className]){
				this[className](p1,p2,p3,p4,p5);
			}	
		}


		ret.prototype[className] = fun;
		ret.prototype.__parentClassName = parentClassName;
		ret.prototype.__className = className;
		ret.__parentClassName = parentClassName;
		ret.__className = className;
		
		/**
		 * 继承
		 */
		ret.__doExtend = function(){
			if(!this.__parentClassName) {
				this.__init();
				return;
			}
			var parentClass = SWClassUtil.getClassByName(this.__parentClassName);
			if(parentClass.__inited){
				var myPrototype = ret.prototype;
				var parentPrototype = parentClass.prototype;
				var needntExtend = SWClassUtil.needntExtend;
				for(var e in parentPrototype ){
					if(!myPrototype[e] && !needntExtend[e]){
						myPrototype[e] = parentPrototype[e];
					}
				}
				
				this.__init();	
			}
		},
		/**
		 * 初始化
		 */
		ret.__init=function(){
			this.__inited = true;
			var children = this.__children;
			if(children){
				
				for(var i=children.length-1;i>=0;i--)
					children[i].__doExtend();
			}
			SWClassUtil.classCnt--;
		}
		
		/**
		 * 增加子类
		 */
		ret.__addChildClass=function(childClass){
			if(!this.__children)
				 this.__children = [];
			this.__children.push(childClass);
		}
		ret.toString = function(){
			return className;
		}
		SWClassUtil.reg(ret);
		return ret;	
	},
	/**
	 * 通过类名得到类
	 */
	getClassByName:function(className){
		if(!className){
			return null;
		}
		var ret = SWClassUtil.classMap[className];
		if(!ret){
			ret = new __SWBlankClass(className);
			SWClassUtil.classMap[className] = ret;
		}
		return ret;
	}
}
/**
 * 空白类
 */
function __SWBlankClass(className){
	this.__className = className;
	this.__children=[];
	
	
}
__SWBlankClass.prototype.__addChildClass=function(childClass){
	this.__children.push(childClass);
}


/**
 * 载入
 */
function $import(jsPath){
	SWImportUtil.finished = false;
	SWImportUtil.addImport(jsPath);
}
/**
 * 继承一个类
 */
function $createClass(className,fun,parentClassName){
	return SWClassUtil.createClass(className,fun,parentClassName)
		
}
function $ready(fun){
	if(SWImportUtil.finished)
		fun();
	else
		SWImportUtil.readFun.push(fun);
}

function $finish(filePath){
	SWImportUtil.finishImport(filePath);
}